# Copyright (c) 2008-2009 solvethis
# Copyright (c) 2010-2011 Casper Ti. Vector
# Public domain.

# 被编译的主文件的文件名，不包括扩展名。
JOBNAME = thesis
# 这个变量的值可以为 latex、pdflatex 或 xelatex。
LATEX = xelatex
BIBTEX = bibtex
DVIPDF = dvipdfmx

TARBALL = $(shell echo thesis_`date +%m%d%Y`_`git log --format=%h -n 1`.tar.gz)
DROPBOX_DIR = /home/brills/Dropbox/thesis
PWD = $(shell pwd)
# 如果用 LaTeX 编译，则使用 dvipdfmx 将 dvi 转成 pdf。
ifeq ($(LATEX), latex)
	DODVIPDF = $(DVIPDF) $(JOBNAME)
endif

# 区分是 Windows 环境还是类 UNIX 环境。
# 如果是后者，则 GNU make 将可以检测到已经定义 PATH 环境变量。
ifdef PATH
	MAKE = make
	RM = rm -f
else
	MAKE = mingw32-make
	RM = del
endif
CONFIG ?= color

color: CONFIG = color
color: TEXINVOKE = $(JOBNAME)
color: all

print: CONFIG = print
print: TEXINVOKE = "\def\printflag{}\input{$(JOBNAME)}"
print: all


all: img doc

tar: 
	@echo Making tarball $(TARBALL)...
	@tar czvf $(TARBALL) *.bib *.tex img ref diagrams $(JOBNAME).pdf Makefile
	@echo Linking to Dropbox...
	@cp -f $(PWD)/$(TARBALL) $(DROPBOX_DIR)


img:
	cd img && $(MAKE) $(CONFIG)

img-clean:
	cd img && $(MAKE) clean



doc:
	$(LATEX) $(TEXINVOKE) 
	$(BIBTEX) $(JOBNAME)
	$(LATEX) $(TEXINVOKE) 
	$(LATEX) $(TEXINVOKE) 
	$(DODVIPDF)

clear:
	$(RM) $(JOBNAME).log $(JOBNAME).aux $(JOBNAME).out $(JOBNAME).thm\
		$(JOBNAME).toc $(JOBNAME).lof $(JOBNAME).lot $(JOBNAME).blg\
		$(JOBNAME).bbl $(JOBNAME).dvi missfont.log
	$(RM) *.aux
	$(RM) *.tar.gz

clean: clear img-clean
	$(RM) $(JOBNAME).pdf

.PHONY: img img-clean clear clean tar

